import filter.HeaderHttpRequestFilter;
import inbound.http.HibServer;
import model.HttpConfig;
import model.Router;

import java.util.*;

/**
 * @author dsp
 * @date 2021-04-04
 */
public class NettyHttpApplication {

    public final static String GATEWAY_NAME = "NettyGateway";
    public final static String GATEWAY_VERSION = "3.0.0";
    public final static int PORT = 8888;
    public final static int WORKER_NUM = 16;

    public static void main(String[] args) {
        System.out.println(GATEWAY_NAME + " " + GATEWAY_VERSION + " starting...");
        // 模拟配置中心加载服务端配置
        HttpConfig config = new HttpConfig();
        config.setPort(PORT);
        config.setMaxContentLength(1024);
        config.setWorkerNum(WORKER_NUM);
        config.setRequestFilters(Collections.singletonList(new HeaderHttpRequestFilter()));
        // routers
        Router router1 = new Router(1, "http://127.0.0.1:8888/demo1/test");
        router1.setServerName("demo1");

        Router router2 = new Router(1, "http://127.0.0.1:8888/demo2/test");
        router1.setServerName("demo2");

        List<Router> routers = new ArrayList<>();
        routers.add(router1);
        routers.add(router2);
        config.setRouters(routers);
        // serverEndpoints
        Map<String/*serverName*/, List<String>/*endpoints*/> map = new HashMap<>();
        map.put("demo1",Arrays.asList("127.0.0.1:8881","127.0.0.1:8882"));
        map.put("demo2",Arrays.asList("127.0.0.1:8885","127.0.0.1:8886"));
        config.setServerEndpoints(map);
        // 初始化服务
        HibServer server = new HibServer(config);
        System.out.println(GATEWAY_NAME + " " + GATEWAY_VERSION + " started at http://127.0.0.1:" + PORT + " for server:" + routers.toString());
        try {
            server.run();
        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println(GATEWAY_NAME + " " + GATEWAY_VERSION + " error...");
        }

    }
}
